package com.hhhn.wk.main.tab4.allorder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hhhn.wk.R;
import com.hhhn.wk.base.BaseActivity;
import com.hhhn.wk.main.tab4.allorder.adapter.MyOrderFragmentAdapter;

import java.lang.reflect.Field;

public class MyOrderActivity extends BaseActivity {
    private TextView tv_title;
    private ViewPager mViewPager1;
    private TabLayout mTabLayout;
    private String[] tabTitle = {"全部", "待支付", "待发货", "待收货","待评价"};//每个页面顶部标签的名字
    private Context context;
    private BaseActivity activity;
    private int type = 0;
    @Override
    protected void setOnCreateContentView() {
        setContentView(R.layout.activity_my_order);
        context = MyOrderActivity.this;
        activity = this;
        Intent intent = getIntent();
        type = intent.getIntExtra("type", 1);
    }
    @Override
    protected void initView() {
        tv_title = findViewById(R.id.tv_title);
        mViewPager1 = (ViewPager) findViewById(R.id.mViewPager1);
        mTabLayout = (TabLayout) findViewById(R.id.mTabLayout);
        tv_title.setText("我的订单");
    }
    @Override
    protected void initData() {
        for (int i = 0; i < tabTitle.length; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(tabTitle[i]));
        }
        mTabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
        //设置顶部标签指示条的颜色和选中页面时标签字体颜色
        mTabLayout.setSelectedTabIndicatorColor(Color.parseColor("#ec0f38"));
        mTabLayout.setTabTextColors(Color.GRAY, Color.parseColor("#ec0f38"));
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabLayout, 0, 0);
            }
        });
        //这里注意的是，因为我是在fragment中创建MyFragmentStatePagerAdapter，所以要传getChildFragmentManager()
        mViewPager1.setAdapter(new MyOrderFragmentAdapter(getSupportFragmentManager(), tabTitle));
        //在设置viewpager页面滑动监听时，创建TabLayout的滑动监听
        mViewPager1.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mViewPager1.setCurrentItem(type);
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //在选中的顶部标签时，为viewpager设置currentitem
                mViewPager1.setCurrentItem(tab.getPosition());
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }
    /**
     * 设置宽度
     *
     * @param tabs
     * @param leftDip
     * @param rightDip
     */
    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }

    }
    @Override
    protected void initClick() {

    }
}
